package cn.kgc.service.impl;

import cn.kgc.constants.Constants;
import cn.kgc.dto.EvaluateDetailDto;
import cn.kgc.dto.EvaluateDto;
import cn.kgc.dto.ModelDto;
import cn.kgc.mapper.EvaluateDetailMapper;
import cn.kgc.mapper.EvaluateMapper;
import cn.kgc.mapper.ModelMapper;
import cn.kgc.pojo.*;
import cn.kgc.service.EvaluateService;
import cn.kgc.service.MalfunctionOptionsService;
import cn.kgc.service.MalfunctionService;
import cn.kgc.utils.redis.RedisKeyUtils;
import cn.kgc.utils.redis.RedisUtils;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 评估信息 服务实现类
 * </p>
 *
 * @author qf
 * @since 2022-03-03
 */
@Service
public class EvaluateServiceImpl extends ServiceImpl<EvaluateMapper, Evaluate> implements EvaluateService {

    private final MalfunctionOptionsService malfunctionOptionsService;
    private final MalfunctionService malfunctionService;
    private final RedisUtils redisUtils;
    private final EvaluateMapper evaluateMapper;
    private final EvaluateDetailMapper evaluateDetailMapper;
    private final ModelMapper modelMapper;


    public EvaluateServiceImpl(MalfunctionOptionsService malfunctionOptionsService, MalfunctionService malfunctionService, RedisUtils redisUtils, EvaluateMapper evaluateMapper, EvaluateDetailMapper evaluateDetailMapper, ModelMapper modelMapper) {
        this.malfunctionOptionsService = malfunctionOptionsService;
        this.malfunctionService = malfunctionService;
        this.redisUtils = redisUtils;
        this.evaluateMapper = evaluateMapper;
        this.evaluateDetailMapper = evaluateDetailMapper;
        this.modelMapper = modelMapper;
    }

    @Override
        public Evaluate assess(Long modelId, String optionIds, String token) {
        String[] optionId = optionIds.split(",");

        BigDecimal sumMoney = new BigDecimal(0);
        //创建EvaluateDetail存入evaluate_detail
        List<EvaluateDetail> evaluateDetailList = new ArrayList<>();

        for (String malfoId : optionId) {
            //根据malfoId查询MalfunctionOptions，获取到ratio和MalfId
            MalfunctionOptions malfunctionOptions = malfunctionOptionsService.getById(malfoId);
            Integer ratio = malfunctionOptions.getRatio();
            Long malfId = malfunctionOptions.getMalfId();
            //根据malfId查询Malfunction，获得最高的维修价格MaintainTopPrice
            Malfunction malfunction = malfunctionService.getById(malfId);
            BigDecimal maintainTopPrice = malfunction.getMaintainTopPrice();

            //计算出该故障的价格
            BigDecimal money = maintainTopPrice.multiply(new BigDecimal(ratio * 0.01));
            System.out.println(malfunction.getTitle() + "," + money);

            //加入到总估价中
            sumMoney = sumMoney.add(money);

            EvaluateDetail evaluateDetail = new EvaluateDetail();
            evaluateDetail.setMalfId(malfId);
            evaluateDetail.setOptionId(malfunctionOptions.getId());
            evaluateDetail.setOptionName(malfunctionOptions.getOptionName());
            evaluateDetailList.add(evaluateDetail);
        }
        //创建evaluate对象
        Evaluate evaluate = new Evaluate();
        evaluate.setModelId(Long.valueOf(modelId));
        evaluate.setSubscription(sumMoney.multiply(new BigDecimal(0.1)));
        evaluate.setPrice(sumMoney);
        //根据token获取用户信息
        String userJson = redisUtils.getValue(RedisKeyUtils.formatKeyWithPrefix(Constants.Redis.PREFIX_TOKEN, token));
        Users users = JSON.parseObject(userJson, Users.class);
        evaluate.setCreatedUserId(users.getId());

        //把数据添加到 评估信息表evaluate中
        evaluateMapper.insert(evaluate);

        //把数据添加到 评估信息详细表evaluate_detail
        for (EvaluateDetail evaluateDetail : evaluateDetailList) {
            evaluateDetail.setEvaluateId(evaluate.getId());
            evaluateDetail.setCreatedUserId(users.getId());

            evaluateDetailMapper.insert(evaluateDetail);
        }

        return evaluate;
    }

    @Override
    public EvaluateDto assessInfo(Long id) {
       EvaluateDto list=null;
       list=evaluateMapper.getevalById(id);
            Long modelId = list.getModelId();
           ModelDto model=modelMapper.getModelId(modelId);
            list.setModel(model);
           List<EvaluateDetailDto> dto=evaluateDetailMapper.getOpnById(id);
           list.setDetailList(dto);
        return list;
    }
}
